// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <pollux/testing/gtest_utils.h>
#include <pollux/testing/functions/prestosql/function_base_test.h>

using namespace kumo::pollux::test;

namespace kumo::pollux::functions {
namespace {

class MapKeyExistsTest : public test::FunctionBaseTest {};

TEST_F(MapKeyExistsTest, general) {
  // numeric key
  auto data = make_row_vector({make_map_vector_from_json<int64_t, int32_t>(
      {"{1:4, 10:5, 3:6}", "{-2:5}", "{}"})});

  auto result = evaluate("map_key_exists(c0, 1)", data);
  auto expected = make_flat_vector<bool>({true, false, false});

  assertEqualVectors(expected, result);

  // string key
  data = make_row_vector({make_map_vector_from_json<std::string, std::string>({
      "{\"ad\":null, \"bc\":null, \"cd\":null}",
  })});

  result = evaluate("map_key_exists(c0, 'bc')", data);
  expected = make_flat_vector<bool>(true);

  assertEqualVectors(expected, result);
}

TEST_F(MapKeyExistsTest, testFloats) {
  auto data = make_row_vector({make_map_vector_from_json<double, int32_t>({
      "{1:10, NaN:20, 0:null, 4:40, 5:50, 6:60}",
      "{NaN:20, -0:20}",
  })});

  auto result = evaluate("map_key_exists(c0, cast('NaN' as REAL))", data);
  auto expected = make_flat_vector<bool>({true, true});

  result = evaluate("map_key_exists(c0, cast('0' as REAL))", data);
  expected = make_flat_vector<bool>({true, false});

  result = evaluate("map_key_exists(c0, cast('-0' as REAL))", data);
  expected = make_flat_vector<bool>({true, true});

  assertEqualVectors(expected, result);
}

} // namespace
} // namespace kumo::pollux::functions
